/*
 * Game_Manager.cpp
 *
 *  Created on: Aug 30, 2012
 *      Author: javu
 */

#include "Game_Manager.h"


Game_Manager::Game_Manager(Screen* GameWindow){
	this->gameWindow = GameWindow;
}

Game_Manager::~Game_Manager(){
	//Delete Screen
}

void Game_Manager::gameControler(){

SDL_Event event;
Uint8 *keystate;
int gameover = 0;

while (!gameover)
{
	//Busca Eventos
	if (SDL_PollEvent(&event))
	{
		//Evento encontrado
		switch (event.type) {
			//Cerrar
			case SDL_QUIT:
				gameover = 1;
				break;

			//Manegador Teclado
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
					case SDLK_ESCAPE:
					case SDLK_q:
						gameover = 1;
						break;
				}
				break;
		}
	}

	//Movimiento de Sprite
	keystate = SDL_GetKeyState(NULL);

	if (keystate[SDLK_LEFT] )
	{
		this->gameWindow->getRcMainSprite()->x -= 2;
	}
	if (keystate[SDLK_RIGHT] )
	{
		this->gameWindow->getRcMainSprite()->x += 2;
	}
	/*
	if (keystate[SDLK_UP] ) {
		rcSprite.y -= 2;
	}
	if (keystate[SDLK_DOWN] ) {
		rcSprite.y += 2;
	}
	*/
	//Colision con bordes
	if ( this->gameWindow->getRcMainSprite()->x < 0 ) {
		this->gameWindow->getRcMainSprite()->x = 0;
	}
	else if ( this->gameWindow->getRcMainSprite()->x > this->gameWindow->getWidth() -SPRITE_SIZE ) {
		this->gameWindow->getRcMainSprite()->x = this->gameWindow->getWidth()-SPRITE_SIZE;
	}
	if ( this->gameWindow->getRcMainSprite()->y < 0 ) {
		this->gameWindow->getRcMainSprite()->y = 0;
	}
	else if ( this->gameWindow->getRcMainSprite()->y > this->gameWindow->getHeight()- SPRITE_SIZE ) {
		this->gameWindow->getRcMainSprite()->y = this->gameWindow->getHeight()-SPRITE_SIZE;
	}

	//Dibujar fondo
	for (int x = 0; x < this->gameWindow->getWidth()/SPRITE_SIZE; x++) {
		for (int y = 0; y < this->gameWindow->getHeight()/SPRITE_SIZE; y++) {
			this->gameWindow->getRcBackGround()->x = x * SPRITE_SIZE;
			this->gameWindow->getRcBackGround()->y = y * SPRITE_SIZE;
			SDL_BlitSurface(this->gameWindow->getBackGround(), NULL, this->gameWindow->getScreen(), this->gameWindow->getRcBackGround());
		}
	}

	//Dibujar Sprite
	SDL_BlitSurface(this->gameWindow->getMainSprite(), NULL, this->gameWindow->getScreen(), this->gameWindow->getRcMainSprite());

	/* update the screen */
	SDL_UpdateRect(this->gameWindow->getScreen(),0,0,0,0);

}
}
